/**
 * Nest请求的三种参数形式
 * @param Param Restful API 参数
 * @param Query Get 请求的 URL 参数
 * @param Body Post 请求的参数
 */
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  UseFilters,
} from '@nestjs/common';
import { AppService } from './app.service';
import { HttpExceptionFilter } from './exception/http-exception.filter';
import { TestService } from './test.service';

// controller主要做路由映射和用户响应 provider主要做业务逻辑的处理
@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    private readonly testService: TestService,
  ) {}

  // get 获取
  // post 插入
  // put 更新
  // delete 删除

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }

  @Get('/data/:subId')
  @UseFilters(new HttpExceptionFilter()) // 绑定异常过滤器
  getData(@Param() Params): object {
    console.log('params:', Params);
    return this.testService.getTest(Params);
  }

  // 二级参数
  @Get('/data/:id/get/:subId')
  getSubData(@Param() Params): string {
    console.log('params:', Params);
    return 'data' + Params.id + Params.subId;
  }

  @Get('/data')
  getAllData() {
    return 'get all data';
  }

  @Post('/data')
  addData(@Body() body, @Query() query) {
    return {
      type: "'add data'",
      data: body,
      query,
    };
  }

  @Put('/data')
  updateData(@Body() body) {
    return {
      type: "'update data'",
      data: body,
    };
  }

  @Delete('/data/:id')
  deleteData(@Param() params) {
    return 'delete data' + params.id;
  }
}
